scraper
HTML parsing and querying with CSS selectors.
scraper
is on Crates.io and GitHub.
Scraper provides an interface to Servo's html5ever
and selectors
crates, for browser-grade parsing and querying.
Examples
Parsing a document
use Html;
let html = r#"
<!DOCTYPE html>
<meta charset="utf-8">
<title>Hello, world!</title>
<h1 class="foo">Hello, <i>world!</i></h1>
"#;
let document = parse_document;
Parsing a fragment
use Html;
let fragment = parse_fragment;
Parsing a selector
use Selector;
let selector = parse.unwrap;
Selecting elements
use ;
let html = r#"
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Baz</li>
</ul>
"#;
let fragment = parse_fragment;
let selector = parse.unwrap;
for element in fragment.select
Selecting descendent elements
use ;
let html = r#"
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Baz</li>
</ul>
"#;
let fragment = parse_fragment;
let ul_selector = parse.unwrap;
let li_selector = parse.unwrap;
let ul = fragment.select.next.unwrap;
for element in ul.select
Accessing element attributes
use ;
let fragment = parse_fragment;
let selector = parse.unwrap;
let input = fragment.select.next.unwrap;
assert_eq!;
Serializing HTML and inner HTML
use ;
let fragment = parse_fragment;
let selector = parse.unwrap;
let h1 = fragment.select.next.unwrap;
assert_eq!;
assert_eq!;
Accessing descendent text
use ;
let fragment = parse_fragment;
let selector = parse.unwrap;
let h1 = fragment.select.next.unwrap;
let text = h1.text.;
assert_eq!;
Contributing
Please feel free to open pull requests. If you're planning on implementing something big (i.e. not fixing a typo, a small bug fix, minor refactor, etc) then please open an issue first.